package org.example.reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @author: ZhuYouBin
 * @date: 2022/4/7 16:00
 * @description:
 */
public class InstanceDemo {
    public static void main(String[] args) throws ClassNotFoundException,
            InstantiationException, IllegalAccessException,
            NoSuchMethodException, InvocationTargetException {
        // 1、创建Class类对象
        Class<?> clazz = Class.forName("org.example.reflection.Student");
        // 2、获取有参构造方法
        Constructor<?> constructor = clazz.getDeclaredConstructor(String.class, String.class);
        // 通过有参构造方法创建对象
        Student student = (Student) constructor.newInstance("参数1", "参数2");
        System.out.println(student);
    }
    static void test01() throws ClassNotFoundException,
            InstantiationException, IllegalAccessException {
        // 1、创建Class类对象
        Class<?> clazz = Class.forName("org.example.reflection.Student");
        // 2、通过 newInstance() 创建对象
        // 通过 Student 类的无参构造方法创建对象
        Student student = (Student) clazz.newInstance();
        System.out.println(student);
    }
}
